package main

import "fmt"

func GetCon(arrs []int, num int) int {
	if len(arrs) == 0 {
		return -1
	}

	if num <= 1 {
		return -1
	}

	mp := map[int]int{} // 这里的k是arr里面的数, value是每个数连续的数量

	for k, v := range arrs {
		preValue := v - 1
		if preNum, ok := mp[preValue]; ok {
			curNum := preNum + 1
			mp[v] = curNum
			if curNum >= num {
				return k
			}

			nextValue := v + 1
			nextConNum := curNum + 1
			for {
				if _, ok := mp[nextValue]; ok {

					mp[nextValue] = nextConNum
					if nextConNum >= num {
						return k
					}

					nextValue++
					nextConNum++
				} else {
					break
				}
			}
		} else {
			mp[v] = 1
		}
	}

	return -1
}

func main() {
	index := GetCon([]int{1, 3, 5, 5, 2, 11, 2, 4}, 5)
	fmt.Println("index:", index)

	nums := []int{1, 3, 5, 7, 2, 11, 9, 4, 3, 2, 5, 6, 1, 7}
	ret := GetCon(nums, 5)
	// fmt.Println(ret)
	if ret != 7 {
		panic("Algorithm logic error")
	} else {
		fmt.Println("index:", ret)
	}

	nums = []int{1, 2, 3, 4, 5, 100, 6, 7}
	ret = GetCon(nums, 7)
	// fmt.Println(ret)
	if ret != 7 {
		panic("Algorithm logic error")
	} else {
		fmt.Println("index:", ret)
	}

	nums = []int{1, 3, 2, 4, 100, 5}
	ret = GetCon(nums, 5)
	// fmt.Println(ret)
	if ret != 5 {
		panic("Algorithm logic error")
	} else {
		fmt.Println("index:", ret)
	}

	nums = []int{1, 3, 2, 4, 100, 101, 5}
	ret = GetCon(nums, 5)
	// fmt.Println(ret)
	if ret != 6 {
		panic("Algorithm logic error")
	} else {
		fmt.Println("index:", ret)
	}

}
